#include "GameScene.h"
#include "../Game/Grid.h"
#include "../Game/GameControl.h"
#include "../Game/Strategy.h"
using namespace cocos2d;
CCScene* GameLayer::m_scene = NULL;

bool GameLayer::init()
{
	CCLayer::init();
	


	CCDirector *pDirector = CCDirector::sharedDirector();

	m_gameWorld = CCTMXTiledMap::tiledMapWithTMXFile("game.tmx");
	CCTMXLayer *ly = m_gameWorld->layerNamed("BG");
	// put map in center
	if (ly == NULL)
	{
		return false;
	}
	CCSize tileSz = m_gameWorld->getTileSize();
	CCSize layerSz = ly->getLayerSize();
	CCSize windowSz = CCDirector::sharedDirector()->getWinSize();
	m_gameWorld->setAnchorPoint(CCPointMake(0.5f, 0.5f));
	m_gameWorld->setScale(1.0f);
	m_gameWorld->setPosition(CCPointMake(windowSz.width / 2, windowSz.height / 2));
	this->addChild(m_gameWorld, 0, 0);
	this->setIsTouchEnabled(true);

	if (m_gameControl == NULL)
	{
		CGrid *grid = new CGrid(layerSz);
		m_gameLayer = m_gameWorld->layerNamed("LEVEL");
		grid->SetCCTMXLayer(m_gameLayer);
		grid->LoadLevel();
		CStrategy *strategy = new CStrategy(grid);
		IGameConfig config;
		config.participatorNum = 2;
		config.playerNum = 2;
		m_gameControl = CGameControl::sharedGameControl();
		m_gameControl->init(grid, strategy, config);
	}
	scheduleUpdate();
	return true;
}

GameLayer::GameLayer()
	: m_gameControl(NULL)
	, m_gameLayer(NULL)
	, m_gameWorld(NULL)
{
	init();

}

CCScene* GameLayer::scene()
{
	if (m_scene == NULL)
	{
		m_scene = CCScene::node();
		GameLayer *layer = new GameLayer();
		m_scene->addChild(layer);
	}
	return m_scene;

}

void GameLayer::update(cocos2d::ccTime dt)
{
	if (m_gameControl->GetGrid()->IsDirty())
	{
		m_gameControl->GetGrid()->RefreshDisplayLayer();

	}
}

void GameLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	//printf("pressed");
}
void GameLayer::ccTouchesEnded(CCSet *touches, CCEvent *pEvent)
{
	//CCPoint firstPos = m_gameLayer->positionAt(CCPointMake(0,0));
	CCSize  gridSize = m_gameLayer->getLayerSize();
	CCPoint firstPos = m_gameWorld->getPosition();
	CCSize  tileSize = CCSizeMake(m_gameWorld->getTileSize().width * m_gameWorld->getScaleX(), m_gameWorld->getTileSize().height * m_gameWorld->getScaleY());
	firstPos.x -=  tileSize.width * m_gameWorld->getMapSize().width / 2;
	firstPos.y -=  tileSize.height * m_gameWorld->getMapSize().height / 2;
	for( CCSetIterator it = touches->begin(); it != touches->end(); ++it)
	{
		CCTouch* touch = (CCTouch*)(*it);
		CCPoint location = touch->locationInView(touch->view());

		//location = CCDirector::sharedDirector()->convertToGL(location);

		for (int i = 0; i < gridSize.width; i++)
			for (int j = 0; j < gridSize.height; j++)
			{
				CCPoint pos = CCPointMake((float)i,(float)j);
				CCRect rect = CCRect(
					firstPos.x + tileSize.width * i,
					firstPos.y + tileSize.height * j,
					tileSize.width,
					tileSize.height);
				if (CCRect::CCRectContainsPoint(rect, location))
				{
					m_gameControl->OnSelected(pos);
					//printf("grid %d, %d selected\n", i, j);
					break;
				}

			}

	}	

//	printf("released");
}